package com.controller;

import com.bean.*;
import com.service.*;
import com.util.DateUtil;
import com.util.PageAssistant;
import com.vo.SalesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

@Component
@Controller
@RequestMapping("buy")
public class BuyReturnController {

    @Autowired
    private BookingService bookingService;
    @Autowired
    private SeatService seatService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private CdyOrderService cdyOrderService;
    @Autowired
    private CinemaService cinemaService;
    @Autowired
    private MovieService movieService;
    @Autowired
    private FilmsService filmsService;

    @RequestMapping("buyTicket")
    public String query(HttpSession session, Model model, Movie movie, PageAssistant<Movie> paMovie, Booking booking){
        System.out.println(">>>>>>>>>>> buyTicket query");
        System.out.println(">>>>>>>>> buy ");
        System.out.println(booking);
        System.out.println(movie);
        System.out.println("-------------------");
        movie.setState(1);
        List<Movie> movies = movieService.find(movie);
        model.addAttribute("movies",movies);
        System.out.println(movies);
        return "page/buyAndReturn/choose";
    }

    @RequestMapping("select")
    public String select(HttpSession session, Model model, Movie movie, PageAssistant<Films> paFilms){
        System.out.println(">>>>>>>>>>> buyTicket select");
        System.out.println(">>>>>>>>> select ");
        System.out.println(movie);
        Map<String,Object> map = new HashMap<>();

        //获取日历工具
        Calendar cal = Calendar.getInstance();
        //获取当前总毫秒数
        long timeInMillis = cal.getTimeInMillis();
        //设置至少提前15分钟买票
        long ticketTime = timeInMillis+15*60*1000;

        map.put("movieId",movie.getId());
        String startDate = DateUtil.format(new Date(), "yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //可以提前买票的最大时间(3天)
        c.add(Calendar.DATE,4);
        String stopDate = DateUtil.format(c.getTime(),"yyyy-MM-dd");
        map.put("startDate",startDate);
        map.put("stopDate",stopDate);

        System.out.println(map.toString());
        //获取所有的选中id的影片
        paFilms.setItems(filmsService.query(map));
        System.out.println(paFilms.getItems().size()+"<><><><><><>查出来的东西");
        int size = paFilms.getItems().size();
        ArrayList<Films> films = new ArrayList<>();
        for (int i = 0; i <size ; i++) {
            //若排片表中影片播放时间小于预设可提前买票的最小时间
            if (paFilms.getItems().get(i).getStartTime().getTime() < ticketTime ){
                System.out.println(ticketTime);
                System.out.println(paFilms.getItems().get(i).getStartTime().getTime());
                //不显示该场电影
                System.out.println(paFilms.getItems());
            }else{
                //当前时间判断可以买票
                films.add(paFilms.getItems().get(i));
                }
        }

        if (films.size() != 0){
            paFilms.setCount(films.size());
            System.out.println("<><><><><><"+paFilms.getCount());
        }else{
            paFilms.setSize(1);
            paFilms.setCount(1);
        }
        System.out.println(paFilms.getItems());
        model.addAttribute("films",films);
        model.addAttribute("paFilms",paFilms);
        return "page/buyAndReturn/filmsList";
    }

    @RequestMapping("show")
    public String show( Model model,Films films){

        System.out.println(films.getId());

        List<Films> films1 = filmsService.find(films);

        model.addAttribute("film",films1.get(0));
        System.out.println("<><><>><><><><<+"+films1.get(0));

        return "page/buyAndReturn/movie";
    }

    @RequestMapping("buyTicketFinal")
    public String buyTicket(Model model,String id,String MovieId,String length,String myseat,String hall){
        System.out.println("买票");
        System.out.println(length);
        System.out.println(myseat);
        System.out.println(hall);
        System.out.println(id);
        System.out.println(MovieId);

        String[] split = myseat.split(",");
        int save = 0;
        for (int i = 0; i <split.length ; i++) {
            String[] split1 = split[i].split("-");
            String hang = split1[0];
            String lie = split1[1];
            //获取到所选的电影信息
            List<Movie> m = movieService.find(new Movie(Integer.valueOf(MovieId)));
            //获取到所选的排片信息
            List<Films> f = filmsService.find(new Films(Integer.valueOf(id)));;
            Booking booking = new Booking();
            //设置订单状态
            booking.setState(2);
            //设置订单行
            booking.setRows(Integer.valueOf(hang));
            //设置订单列
            booking.setNumber(Integer.valueOf(lie));
            //设置订单电话
            booking.setPhone("15735182312");
            //设置订单影厅号
            booking.setHall(Integer.valueOf(hall));
            //设置订单影片开始时间
            booking.setTime(f.get(0).getStartTime());
            //设置订单下单时间
            booking.setOrderTime(new Date());
            //设置订单价格
            booking.setPrice(f.get(0).getPrice());
            booking.setMovieName(MovieId);
            bookingService.save(booking);
            save += 1;
            System.out.println("添加的电影票订单数: "+ save);
        }
        //获取到所选的排片信息
        List<Films> f = filmsService.find(new Films(Integer.valueOf(id)));
        Double subtotal = save*f.get(0).getPrice();

        return "redirect:/alipayTicket?"+"&subtotal="+subtotal;
    }

    @RequestMapping("buyCommodity")
    public String buyCommodity(HttpSession session, Commodity commodity,Model model,PageAssistant<Commodity> paCommodity, CdyOrder cdyOrder){
        System.out.println(">>>>>>>>>>> buyCommodity query");
        System.out.println(">>>>>>>>> buy ");

        System.out.println("-------------------");
        commodity.setState(0);
        List<Commodity> commodities = commodityService.find(commodity);
        model.addAttribute("commodities",commodities);
        System.out.println(commodities);
        return "page/buyAndReturn/chooseCommodity";
    }

    @RequestMapping("selectCommodity")
    public String selectCommodity(HttpSession session, Commodity commodity,Model model, CdyOrder cdyOrder){
        System.out.println(">>>>>>>>>>> selectCommodity query");
        System.out.println(">>>>>>>>> buy ");

        System.out.println("-------------------");
        commodity.setState(0);
        List<Commodity> commodities = commodityService.find(commodity);
        model.addAttribute("commodities",commodities);
        System.out.println(commodities);
        return "page/buyAndReturn/chooseCommodity";
    }

    @RequestMapping("returnCommodity")
    public  String returnCommodity(){
        return "page/buyAndReturn/returnCommodity";
    }

    @Transactional
    @RequestMapping("checkCommodity")
    public String checkCommodity(HttpSession session, Model model, CdyOrder cdyOrder, Commodity commodity) {
        System.out.println("checkCommodity");
        System.out.println(cdyOrder);
        System.out.println(model);
        System.out.println(commodity);
        System.out.println("---------------------");
        //查询订单相关
        List<CdyOrder> cdyOrders = cdyOrderService.find(cdyOrder);
        if (cdyOrders.size() > 0){
            CdyOrder c = cdyOrders.get(0);
            //修改库存
            Date date = new Date();

            if (c.getOrderTime().getTime()+ 60*60*1000 < date.getTime()||c.getState() == 1 || c.getState() == 2){
                System.out.println("对不起,超过一小时的商品订单或者该订单已经退货成功,或者已经参加过活动,我们不予办理!");
                return "page/buyAndReturn/false";
            }else{
                commodity.setId(c.getCdyName());
                //查询到要修改的commodity信息
                List<Commodity> commodities = commodityService.find(commodity);
                Commodity co = commodities.get(0);
                System.out.println(co+"修改前");
                int num = co.getStock()+c.getNumber();
                co.setStock(num);
                System.out.println(co+"修改后");
                int modify = commodityService.modify(co);
                c.setState(1);
                int modify1 = cdyOrderService.modify(c);
                System.out.println(modify+"1成功");
                System.out.println(modify1+"1成功");
                return "page/buyAndReturn/ok";
            }
        }else{
            System.out.println("对不起,没有该订单,我们不予办理!");
            return "page/buyAndReturn/false";
        }
    }

    @RequestMapping("returnTicket")
    public String returnTicket(){
        return "page/buyAndReturn/returnTicket";
    }

    @Transactional
    @RequestMapping("checkTicket")
    public String checkTicket(HttpSession session, Model model, Booking booking, Movie movie) {
        System.out.println("checkTicket");
        System.out.println(movie);
        System.out.println(model);
        System.out.println(booking);
        System.out.println("---------------------");
        //查询订单相关
        List<Booking> bookings = bookingService.find(booking);
        if (bookings.size() > 0){
            Booking b = bookings.get(0);
            //修改库存
            Date date = new Date();

            if (b.getOrderTime().getTime()+ 10*60*1000 < b.getTime().getTime()|| b.getState() == 1 || b.getState() == 2){
                System.out.println("对不起,提前10分钟退票或者您的票已经受理过退票,或者没有该订单信息,或者参加过活动,我们不予办理!");
                return "page/buyAndReturn/false";
            }else{
                //修改座位相关信息
            }
            return "page/buyAndReturn/ok";
        }else{
            return "page/buyAndReturn/false";
        }
    }
}
